﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using Services.BusinessRules;
namespace Services
{

    public partial class VoucherService
    {
       readonly IVoucherEntity entity;
       readonly IVwCashBookEntity VwCashBookEntity;
       public VoucherService()
       {
           this.entity = new VoucherEntity();
           VwCashBookEntity = new VwCashBookEntity();

       }

       
       #region Sqlcommand
        /// <summary>
        /// code thu hoc phi
        /// </summary>
       string GetMaxReceiptNo = "select top 1 ReceiptNo from Voucher where Type=0 and ReceiptNo like 'PTHP%' order by ReceiptNo desc ";
        /// <summary>
        /// code thu can ho
        /// </summary>
       string GetMaxReceiptNoApartment = "select top 1 ReceiptNo from Voucher where Type=0  and ReceiptNo like 'PTCH%' order by ReceiptNo desc ";
       /// code thu noi bo
       /// </summary>
       string GetMaxReceiptNoLocal = "select top 1 ReceiptNo from Voucher where Type=0 and ReceiptNo like 'PTNB%' order by ReceiptNo desc ";
       /// code thu salon
       /// </summary>
       string GetMaxReceiptNoSalon = "select top 1 ReceiptNo from Voucher where Type=0  and ReceiptNo like 'PTSL%' order by ReceiptNo desc ";

       /// code thu ban hang
       /// </summary>
       string GetMaxReceiptNoSales = "select top 1 ReceiptNo from Voucher where Type=0  and ReceiptNo like 'PTDC%' order by ReceiptNo desc ";


       /// code chi căn họ
       /// </summary>
       string GetMaxPaymentNoApartment = "select top 1 ReceiptNo from Voucher where Type=1  and ReceiptNo like 'PCCH%' order by ReceiptNo desc ";

       /// code chi trường
       /// </summary>
       string GetMaxPaymentNoSchool = "select top 1 ReceiptNo from Voucher where Type=1  and ReceiptNo like 'PCT%' order by ReceiptNo desc ";


       #endregion

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model, ref string mess)
       {
           try
           {
               if (Validate((Voucher)_model, ref mess))
               {

                   var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
                   if (id == 0)
                   {
                       return entity.Save((CoreData.Voucher)_model, Table.Voucher.ToString());
                   }
                   else
                   {
                       return entity.Update((CoreData.Voucher)_model, Table.Voucher.ToString());
                   }
               }
               else {
                   return -1;
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Voucher GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.Voucher.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Voucher> GetList()
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetReceiptVoucher, " Voucher.IsDeleted ='false' ")).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Voucher> GetListByLINQ(Func<CoreData.Voucher, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Voucher.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Voucher GetOneByLINQ(Func<CoreData.Voucher, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Voucher.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Voucher> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetReceiptVoucher, " Voucher.IsDeleted ='false' and " + _searchstring)).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Voucher)_model, Table.Voucher.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion



        #region Other Method


       bool Validate(Voucher obj, ref string mess)
       {
           new ValidateRequired("Mã phiếu không được rỗng","", obj.ReceiptNo);
           new ValidateID("Phương thức thanh toán", "Payment Method", obj.PaymentMethod);
           new ValidateID("Lý do thu", "Name", obj.ReceiptReason);
         
           mess = BusinessObject.Validate();
           BusinessObject.Clear();
           return mess == null ? true : false;
       }

       const string CodeThuHocPhi = "PTHP"; //dinh dang PTHPngaythangnamsotutang
       const string CodeThuCanHo = "PTCH";
       const string CodeThuNoiBo = "PTNB";
       const string CodeThuSalon = "PTSL";
       const string CodeChiCanHo = "PCCH";
       const string CodeChiTruong = "PCT";
       const string CodeThuBanHang = "PTDC";
        /// <summary>
        /// Code thu hoc phi
        /// </summary>
        /// <returns></returns>
       public string GenerateReceiptNo(DateTime createdate)
       {
           //string day = createdate.Day.ToString();
           //string month = createdate.Month.ToString();
           //string year = createdate.Year.ToString().Substring(createdate.Year.ToString().Length - 2);
           //if(day.Length < 2)
           //{
           //  day = day.Insert(0, "0");
           //}
           //if(month.Length < 2)
           //{
           //  month = month.Insert(0, "0");
           //}
           string code = CodeThuHocPhi;// +day + month + year;
           try
           {             
              var data = entity.GetByCusTomSQL(GetMaxReceiptNo).FirstOrDefault();
              if (String.IsNullOrEmpty(data.ReceiptNo))
                  data.ReceiptNo = "0";
              string temp = data.ReceiptNo.Substring(4);
              int autonum = int.Parse(temp) + 1;
               if(autonum<=9)
               {
                   code =code + "000" + autonum.ToString();
               }
               else if (autonum <= 99 && autonum>9)
               {
                   code = code + "00" + autonum.ToString();
               }
               else if (autonum <= 999 && autonum>99)
               {
                   code = code + "0" + autonum.ToString();
               }
               else
               {
                   code = code + autonum.ToString();
               }
           }
           catch
           {
               code = code + "0001";
           }
           return code;
       }


        /// <summary>
        /// Code thu can ho
        /// </summary>
        /// <returns></returns>
       public string GenerateReceiptNoApartment(DateTime createdate)
       {
           //string day = createdate.Day.ToString();
           //string month = createdate.Month.ToString();
           //string year = createdate.Year.ToString().Substring(createdate.Year.ToString().Length - 2);
           //if (day.Length < 2)
           //{
           //    day = day.Insert(0, "0");
           //}
           //if (month.Length < 2)
           //{
           //    month = month.Insert(0, "0");
           //}
           string code = CodeThuCanHo;// +day + month + year;
           try
           {
               var data = entity.GetByCusTomSQL(GetMaxReceiptNoApartment).FirstOrDefault();
               if (String.IsNullOrEmpty(data.ReceiptNo))
                   data.ReceiptNo = "0";
               string temp = data.ReceiptNo.Substring(4);
               int autonum = int.Parse(temp) + 1;
               if (autonum <= 9)
               {
                   code = code + "000" + autonum.ToString();
               }
               else if (autonum <= 99 && autonum > 9)
               {
                   code = code + "00" + autonum.ToString();
               }
               else if (autonum <= 999 && autonum > 99)
               {
                   code = code + "0" + autonum.ToString();
               }
               else
               {
                   code = code + autonum.ToString();
               }
           }
           catch
           {
               code = code + "0001";
           }
           return code;
       }


       /// <summary>
       /// Code thu noi bo
       /// </summary>
       /// <returns></returns>
       public string GenerateReceiptNoLocal(DateTime createdate)
       {
           //string day = createdate.Day.ToString();
           //string month = createdate.Month.ToString();
           //string year = createdate.Year.ToString().Substring(createdate.Year.ToString().Length - 2);
           //if (day.Length < 2)
           //{
           //    day = day.Insert(0, "0");
           //}
           //if (month.Length < 2)
           //{
           //    month = month.Insert(0, "0");
           //}
           string code = CodeThuNoiBo;// +day + month + year;
           try
           {
               var data = entity.GetByCusTomSQL(GetMaxReceiptNoLocal).FirstOrDefault();
               if (String.IsNullOrEmpty(data.ReceiptNo))
                   data.ReceiptNo = "0";
               string temp = data.ReceiptNo.Substring(4);
               int autonum = int.Parse(temp) + 1;
               if (autonum <= 9)
               {
                   code = code + "000" + autonum.ToString();
               }
               else if (autonum <= 99 && autonum > 9)
               {
                   code = code + "00" + autonum.ToString();
               }
               else if (autonum <= 999 && autonum > 99)
               {
                   code = code + "0" + autonum.ToString();
               }
               else
               {
                   code = code + autonum.ToString();
               }
           }
           catch
           {
               code = code + "0001";
           }
           return code;
       }


       /// <summary>
       /// Code thu Salon
       /// </summary>
       /// <returns></returns>
       public string GenerateReceiptNoSalon(DateTime createdate)
       {
           //string day = createdate.Day.ToString();
           //string month = createdate.Month.ToString();
           //string year = createdate.Year.ToString().Substring(createdate.Year.ToString().Length - 2);
           //if (day.Length < 2)
           //{
           //    day = day.Insert(0, "0");
           //}
           //if (month.Length < 2)
           //{
           //    month = month.Insert(0, "0");
           //}
           string code = CodeThuSalon;// +day + month + year;
           try
           {
               var data = entity.GetByCusTomSQL(GetMaxReceiptNoSalon).FirstOrDefault();
               if (String.IsNullOrEmpty(data.ReceiptNo))
                   data.ReceiptNo = "0";
               string temp = data.ReceiptNo.Substring(4);
               int autonum = int.Parse(temp) + 1;
               if (autonum <= 9)
               {
                   code = code + "000" + autonum.ToString();
               }
               else if (autonum <= 99 && autonum > 9)
               {
                   code = code + "00" + autonum.ToString();
               }
               else if (autonum <= 999 && autonum > 99)
               {
                   code = code + "0" + autonum.ToString();
               }
               else
               {
                   code = code + autonum.ToString();
               }
           }
           catch
           {
               code = code + "0001";
           }
           return code;
       }

       /// <summary>
       /// Code thu ban hang
       /// </summary>
       /// <returns></returns>
       public string GenerateReceiptNoSales(DateTime createdate)
       {
           //string day = createdate.Day.ToString();
           //string month = createdate.Month.ToString();
           //string year = createdate.Year.ToString().Substring(createdate.Year.ToString().Length - 2);
           //if (day.Length < 2)
           //{
           //    day = day.Insert(0, "0");
           //}
           //if (month.Length < 2)
           //{
           //    month = month.Insert(0, "0");
           //}
           string code = CodeThuBanHang;// +day + month + year;
           try
           {
               var data = entity.GetByCusTomSQL(GetMaxReceiptNoSales).FirstOrDefault();
               if (String.IsNullOrEmpty(data.ReceiptNo))
                   data.ReceiptNo = "0";
               string temp = data.ReceiptNo.Substring(4);
               int autonum = int.Parse(temp) + 1;
               if (autonum <= 9)
               {
                   code = code + "000" + autonum.ToString();
               }
               else if (autonum <= 99 && autonum > 9)
               {
                   code = code + "00" + autonum.ToString();
               }
               else if (autonum <= 999 && autonum > 99)
               {
                   code = code + "0" + autonum.ToString();
               }
               else
               {
                   code = code + autonum.ToString();
               }
           }
           catch
           {
               code = code + "0001";
           }
           return code;
       }



        /// <summary>
       /// Code chi can
       /// </summary>
       /// <returns></returns>
       public string GeneratePaymentNoApartment(DateTime createdate)
       {
           //string day = createdate.Day.ToString();
           //string month = createdate.Month.ToString();
           //string year = createdate.Year.ToString().Substring(createdate.Year.ToString().Length - 2);
           //if (day.Length < 2)
           //{
           //    day = day.Insert(0, "0");
           //}
           //if (month.Length < 2)
           //{
           //    month = month.Insert(0, "0");
           //}
           string code = CodeChiCanHo;// +day + month + year;
           try
           {
               var data = entity.GetByCusTomSQL(GetMaxPaymentNoApartment).FirstOrDefault();
               if (String.IsNullOrEmpty(data.ReceiptNo))
                   data.ReceiptNo = "0";
               string temp = data.ReceiptNo.Substring(4);
               int autonum = int.Parse(temp) + 1;
               if (autonum <= 9)
               {
                   code = code + "000" + autonum.ToString();
               }
               else if (autonum <= 99 && autonum > 9)
               {
                   code = code + "00" + autonum.ToString();
               }
               else if (autonum <= 999 && autonum > 99)
               {
                   code = code + "0" + autonum.ToString();
               }
               else
               {
                   code = code + autonum.ToString();
               }
           }
           catch
           {
               code = code + "0001";
           }
           return code;
       }

       /// <summary>
       /// Code chi truong
       /// </summary>
       /// <returns></returns>
       public string GeneratePaymentNoSchool(DateTime createdate)
       {
           //string day = createdate.Day.ToString();
           //string month = createdate.Month.ToString();
           //string year = createdate.Year.ToString().Substring(createdate.Year.ToString().Length - 2);
           //if (day.Length < 2)
           //{
           //    day = day.Insert(0, "0");
           //}
           //if (month.Length < 2)
           //{
           //    month = month.Insert(0, "0");
           //}
           string code = CodeChiTruong;// +day + month + year;
           try
           {
               var data = entity.GetByCusTomSQL(GetMaxPaymentNoSchool).FirstOrDefault();
               if (String.IsNullOrEmpty(data.ReceiptNo))
                   data.ReceiptNo = "0";
               string temp = data.ReceiptNo.Substring(3);
               int autonum = int.Parse(temp) + 1;
               if (autonum <= 9)
               {
                   code = code + "000" + autonum.ToString();
               }
               else if (autonum <= 99 && autonum > 9)
               {
                   code = code + "00" + autonum.ToString();
               }
               else if (autonum <= 999 && autonum > 99)
               {
                   code = code + "0" + autonum.ToString();
               }
               else
               {
                   code = code + autonum.ToString();
               }
           }
           catch
           {
               code = code + "0001";
           }
           return code;
       }

        #endregion 


        #region Report

       public IList<VwCashBook> GetListCashBook()
       {
           try {
               return VwCashBookEntity.GetAll(Table.VwCashBook.ToString()).ToList();
           }
           catch {
               return null;
           }
       }
       public IList<VwCashBook> GetListCashBook(string SearchString)
       {
           try
           {
               return VwCashBookEntity.GetBySearchString(SearchString,Table.VwCashBook.ToString()).ToList();
           }
           catch
           {
               return null;
           }
       }
        #endregion

    }
         
}
